package com.ssm.studen.controller;

import com.ssm.studen.model.ClassRoom;
import com.ssm.studen.service.ClassRoomService;
import com.ssm.studen.tool.ResponseMessage;
import com.ssm.studen.tool.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 班级：
 *      对外提供数据服务接口
 */
@Api(tags = "classRoom")
@RestController
public class ClassRoomController {

    @Autowired
    private ClassRoomService classRoomService;


    /**********************班级添加方法*************************/
    @ApiOperation("班级添加方法")
    @RequestMapping(value = "/addClassRoom",method = RequestMethod.GET)
    public int addClassRoom(ClassRoom classRoom){
        int IsAdd = 0;
        //传入数据判断
        //1.实体班级类不能为空
        if(classRoom != null){
            //2.班级名称不能为空
            if(classRoom.getCname() != null){
                //3.添加角色数据
                IsAdd = classRoomService.addClassRoom(classRoom);
            }
        }
        return IsAdd;
    }


    /**********************班级删除方法*************************/
    @ApiOperation("班级删除方法")
    @RequestMapping(value = "/delectClassRoom",method = RequestMethod.GET)
    public ResponseMessage delectClassRoom(int cnum){
        if(cnum != 0){
            int delInt = 0;
            delInt = classRoomService.delectClassRoom(cnum);
            if(delInt>0){
                return Result.success("删除成功");
            }else{
                return Result.error("删除失败");
            }
        }else{
            return Result.error("删除的主键不能为“0”，请重新输入！");
        }
    }


    /**********************班级更新方法*************************/
    @ApiOperation("班级更新方法")
    @RequestMapping(value = "/updateClassRoom",method = RequestMethod.GET)
    public ResponseMessage updateClassRoom(ClassRoom classRoom){
        //传入的参数不能为空
        if(classRoom != null){
            //参数当中主键不能为0
            if(classRoom.getCnum() != 0){
                if(classRoomService.updateClassRoom(classRoom) == 1){
                    if(classRoom.getCschool() != null){
                        classRoomService.updateClassRoom(classRoom);
                    }
                    return Result.success("更新成功");
                }else{
                    return Result.error("更新失败");
                }
            }
        }
        return Result.error("传入参数不正确");
    }


    /**********************班级查询方法*************************/
    @ApiOperation("班级查询方法")
    @RequestMapping(value = "/selectClassRoom",method = RequestMethod.PUT)
    public ResponseMessage selectClassRoom(int cnum){
        Map<String,List> map = new HashMap<>();
        List<ClassRoom> listClassRoom = classRoomService.selectClassRoom(cnum);
        map.put("list",listClassRoom);
        return Result.success("200",map);
    }
}
















